<%@ page language="java" contentType="text/html; charset=gbk" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.UUID" %>
<%
    JSONObject jsonData = new JSONObject();
    int dataid = 0;
    String requestid = Util.null2String(request.getParameter("requestid"));
    BaseBean baseBean = new BaseBean();
    baseBean.writeLog(">>>>>getWlxjd.jsp>>>>>requestid="+requestid);
    rs.executeQuery("select id,sqr,bm,sqrq,sfjsbj from formtable_main_356 where requestid='"+requestid+"'");
    rs.next();
    String mainid = Util.null2String(rs.getString("id"));
    String sqr = Util.null2String(rs.getString("sqr"));
    String bm = Util.null2String(rs.getString("bm"));
    String sqrq = Util.null2String(rs.getString("sqrq"));
    String sfjsbj = Util.null2String(rs.getString("sfjsbj"));
    baseBean.writeLog(">>>>>getWlxjd.jsp>>>>>mainid="+mainid+",sqr="+sqr+",bm="+bm+",sqrq="+sqrq);
    rs.execute("select * from formtable_main_356_dt1 where mainid='"+mainid+"'");
    if (rs.next()) {
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("requestId", requestid);
        dataMap.put("sqr", sqr);
        dataMap.put("bm", bm);
        dataMap.put("sqrq", sqrq);
        int newbjsc = 1;
        String bjsc = getBjcs(requestid);
        if(!"".equals(bjsc)){
            newbjsc = Util.getIntValue(bjsc)+1;
        }
        dataMap.put("bjcs", newbjsc);
        dataMap.put("bh", Util.null2String(rs.getString("bh")));
        dataMap.put("wlmcxxgg", Util.null2String(rs.getString("wlmcxxgg")));
        dataMap.put("bcsm", Util.null2String(rs.getString("bcsm")));
        dataMap.put("sliang", Util.null2String(rs.getString("sliang")));
        dataMap.put("dwmm", Util.null2String(rs.getString("dwmm")));
        dataMap.put("djjmf", Util.null2String(rs.getString("djjmf")));
        dataMap.put("pcbz", Util.null2String(rs.getString("pcbz")));
        dataMap.put("jgydpl", Util.null2String(rs.getString("jgydpl")));
        dataMap.put("sfecjg", Util.null2String(rs.getString("sfecjg")));
        dataMap.put("yxq", Util.null2String(rs.getString("yxq")));
        dataMap.put("zzcp", Util.null2String(rs.getString("zzcp")));
        dataMap.put("kh", Util.null2String(rs.getString("kh")));
        dataMap.put("sl", Util.null2String(rs.getString("sl")));
        dataMap.put("ylzssm", Util.null2String(rs.getString("ylzssm")));
        dataMap.put("qmqq", Util.null2String(rs.getString("qmqq")));
        dataMap.put("ssl", Util.null2String(rs.getString("ssl")));
        dataMap.put("bcsx", Util.null2String(rs.getString("bcsx")));
        dataMap.put("fjsc", Util.null2String(rs.getString("fjsc")));
        dataMap.put("bjr", Util.null2String(rs.getString("bjr")));
        dataMap.put("sbje", Util.null2String(rs.getString("sbje")));
        dataMap.put("zt", sfjsbj);
        baseBean.writeLog(">>>>>getWlxjd.jsp>>>>>dataMap="+dataMap);
        dataid =  insertModelData(285, 1, dataMap);
    }
    baseBean.writeLog(">>>>>getWlxjd.jsp>>>>>dataid="+dataid);
    jsonData.put("dataid", dataid);
    out.print(jsonData.toJSONString());
%>
<%!

    public static void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }


    public static int insertModelData(int modeid, int userid, Map<String,Object> map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id="+modeid;
        rs.execute(sql);
        rs.next();
        String tablename = Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)" +
                "values('"+uuid+"','"+userid+"','"+TimeUtil.getCurrentDateString()+"','"+TimeUtil.getOnlyCurrentTimeString()+"','"+modeid+"')";
        if (rs.execute(insSql)) {
            rs1.execute("select id from " + tablename + " where uuid='"+uuid+"'");
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                newModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }

    public static String getBjcs(String requestid) {
        RecordSet rs = new RecordSet();
        rs.execute("select max(bjcs) from uf_wlxjdtz where requestid="+requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
%>